//
//  TopListViewController.m
//  XMLY
//
//  Created by 曾杰 on 15/11/7.
//  Copyright © 2015年 曾杰. All rights reserved.
//

#import "TopListViewController.h"
#import "PKRequestManager.h"
#import "SoundBookModel.h"
#import "TopListCell.h"
#import "UIImageView+WebCache.h"
#import "MJRefresh.h"
#import "MusicDetailViewController.h"
@interface TopListViewController ()<UITableViewDataSource,UITableViewDelegate>
{
    NSInteger i;
    NSInteger n;
}
@property (nonatomic, strong)NSMutableArray *dataArr;
@property (nonatomic, strong)UITableView *topListTable;
@end

@implementation TopListViewController

-(NSMutableArray *)dataArr
{
    if (_dataArr == nil) {
        self.dataArr = [NSMutableArray arrayWithCapacity:0];
    }
    return _dataArr;
}
-(void)requestData
{
    
    NSString * urlStr =[NSString stringWithFormat:@"http://mobile.ximalaya.com/mobile/discovery/v1/rankingList/album?device=iPhone&key=%@&pageId=%ld&pageSize=20&position=0",_key,n];
    
    [PKRequestManager requestWithType:GET urlString:urlStr parDic:nil finish:^(NSData *data) {
        
     
          NSDictionary *dic = [NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingMutableContainers error:nil];
               NSArray *arr = dic[@"list"];
        for (NSDictionary *listDic in arr) {
            SoundBookModel *model = [[SoundBookModel alloc]init];
            [model setValuesForKeysWithDictionary:listDic];
            [self.dataArr addObject:model];
        }
        [self.topListTable.header endRefreshing];
        [self.topListTable.footer endRefreshing];
        [self.topListTable reloadData];
      

    } error:^(NSError *error) {
        
    }];
}

- (void)viewDidLoad {
    [super viewDidLoad];
    //网络请求
    n = 1;
    [self requestData];
    
    //创建tableview
    [self createTopTable];
    
    //注册cell
    [self.topListTable registerClass:[TopListCell class] forCellReuseIdentifier:@"topLsitCell"];
    //刷新
    [self createRefresh];
    //标题视图
    [self createTopView];
}
//标题视图
- (void)createTopView
{
    UILabel *topLabel = [[UILabel alloc]initWithFrame:CGRectMake(SCREEN_WIDTH/2-100, 20, 200, 30)];
    topLabel.text = _titleStr;
    topLabel.textAlignment = NSTextAlignmentCenter;
    [self.view addSubview:topLabel];
    UIButton *returnButton = [UIButton buttonWithType:(UIButtonTypeCustom)];
    returnButton.frame = CGRectMake(5, 25, 20, 20);
    [returnButton setImage:[UIImage imageNamed:@"fanhui"] forState:(UIControlStateNormal)];
    [returnButton addTarget:self action:@selector(dismissView) forControlEvents:(UIControlEventTouchUpInside)];
    [self.view addSubview:returnButton];
    
}
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}
//返回
- (void)dismissView{
    [self dismissViewControllerAnimated:YES completion:nil];
}
//创建tableView
- (void)createTopTable
{
    self.topListTable = [[UITableView alloc]initWithFrame:CGRectMake(0, 64, SCREEN_WIDTH, SCREEN_HEIGHT-64)];
    self.topListTable.delegate = self;
    self.topListTable.dataSource = self;
    self.view.backgroundColor = [UIColor whiteColor];
    [self.view addSubview:_topListTable];

}

#pragma mark - Table view data source


//行数
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return self.dataArr.count;
}

//cell的配置
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    TopListCell *cell = [tableView dequeueReusableCellWithIdentifier:@"topLsitCell" forIndexPath:indexPath];
    SoundBookModel *model = self.dataArr[indexPath.row];
    cell.topLabel.text = [NSString stringWithFormat:@"%ld",(long)indexPath.row+1];
    [cell.picImageView sd_setImageWithURL:[NSURL URLWithString:model.albumCoverUrl290]];
    cell.titleLabel.text = model.title;
    cell.introLabel.text = model.intro;
    cell.tracksCounts.text = [[NSString stringWithFormat:@"%ld",(long)model.tracksCounts] stringByAppendingString:@"集"];
    return cell;
}
//行高
-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return 100;
}
//点击跳转
-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
   MusicDetailViewController *playListVC = [[MusicDetailViewController alloc]init];
    SoundBookModel *model = self.dataArr[indexPath.row];
    NSLog(@"%ld",(long)model.albumId);
    playListVC.albumId = [NSString stringWithFormat:@"%ld",model.albumId];
    [self presentViewController:playListVC animated:YES completion:nil];
}
//刷新
- (void)createRefresh
{
    self.topListTable.header = [MJRefreshNormalHeader headerWithRefreshingBlock:^{
        [self.dataArr removeAllObjects];
        n = 1;
        [self requestData];
    }];
    self.topListTable.footer = [MJRefreshAutoNormalFooter footerWithRefreshingBlock:^{
        n += 1;
        [self requestData];
    }];

}

@end
